import createMatchRoute from '../utils/index.js';

export default {
  name: 'RouterView',
  render(h) {
    // this._self VueComponent
    const { routeMap, data } = this.$router;
    if (routeMap && data && data.current) {
      const routeItem = routeMap[data.current];
      const { path, matched } = createMatchRoute(routeItem, data.current);

      // Component - RouterView - this
      this.routerView = true;
      let deepth = 0, parent = this.$parent;

      /**
       * 避免无限循环渲染
       * 根实例:new Vue()
       * App.vue
       * 组件:RouterView - this
       * 依次类推
       */
      while (parent) {
        if (parent.routerView) {
          deepth++;
        }
        parent = parent.$parent;
      }

      let singleItem = matched[deepth];

      /**
       * h()
       * 生成注释节点
       */
      return singleItem
        ? h(singleItem.component) :
        h();
    }
  }
};
